package com.tyc.mb.common.scheduler;

import com.tyc.mb.exception.BaseException;
import com.tyc.mb.service.CommonService;
import com.tyc.mb.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 定时任务
 *
 * @author tyc
 * @version 1.0
 * @date 2021-11-02 15:18:51
 */

@Slf4j
@Component
@EnableScheduling
public class SchedulerTask {
    @Autowired
    private CommonService commonService;

    /**
     * 前一个月创建好下个月要使用的的b
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void dayDataEsSave() {
        log.info("-------------------------------- createTable ------------------------ >");
        String oldTable = "user";
        LocalDate now = LocalDate.now();
        LocalDate nextMonth = now.plusMonths(1);
        String tableDate = nextMonth.format(DateTimeFormatter.ofPattern("_yyyy_MM"));
        String tableName = oldTable+tableDate;
        try {
            commonService.createTable(tableName, oldTable);
        } catch (Exception e) {
            log.error("createTable {} error ===> {}", tableName, e.getMessage());
        }
    }
}
